/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.ShopFacade;
import dbHandling.DatabaseConnect;
import java.awt.Toolkit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import jpamodel.CommissionWare;
import jpamodel.OwnedWare;
import jpamodel.Seller;

/**
 *
 * @author Hans
 */
public class MainView extends Application {

    public static final double WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final double HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    public static final Insets INSETS = new Insets(20, 10, 20, 10);
    public static final double SPACING = 5;
    public static final String PUKE_GREEN = "#c6e68f";
    private final CommissionsView commisions;
    private final OwnedWaresView ownedWares;
    private final NewWareView newWareView;
    private final NewSellerView newSellerView;
    private StackPane root = new StackPane();
    private BorderPane borderPane = new BorderPane();
    private Canvas background = new Canvas(WIDTH, HEIGHT);
    private Image bgImage = new Image("images/bg.jpg");
    private final SellersView sellersView;
    private Scene scene = new Scene(root, 1_024, 768);
    private static Font font;
    public static final ObservableList<Seller> sellerList = FXCollections.observableArrayList();
    public static final ObservableList<CommissionWare> commissionList = FXCollections.observableArrayList();
    public static final ObservableList<OwnedWare> ownedList = FXCollections.observableArrayList();
    private ShopFacade shop;
    private static boolean debug;
    private DatabaseView dbView;


    public MainView() {
        font = null;
        String url = "/resources/girlsare.ttf";
        font = Font.loadFont(MainView.class.getResourceAsStream(url), 30);
        int space = 12;
        DatabaseConnect dbc = new DatabaseConnect(debug);
        shop = new ShopFacade(dbc);
        try {
            dbc.connectToDB();
            sellerList.addAll(shop.getAllSellers());
            commissionList.addAll(shop.getAllCommissionWares());
            ownedList.addAll(shop.getAllOwnedWares());
        } catch (Exception e) {
            dbView = new DatabaseView(borderPane, dbc, e);
            borderPane.setCenter(dbView);
        }
        this.sellersView = new SellersView(sellerList, shop);
        sellersView.setSpacing(space);
        this.commisions = new CommissionsView(commissionList, sellerList, shop);
        commisions.setSpacing(space);
        this.ownedWares = new OwnedWaresView(ownedList, shop);
        ownedWares.setSpacing(space);
        this.newWareView = new NewWareView(sellerList, ownedList, commissionList, shop);
        this.newSellerView = new NewSellerView(sellerList, shop);
    }

    public static Font getFont() {
        return font;
    }

    @Override
    public void start(Stage primaryStage) {

        root.setAlignment(Pos.TOP_LEFT);
        root.getChildren().add(background);
        root.getStylesheets().add(MainView.class.getResource("/resources/listView.css").toExternalForm());
        setBackGround();
        setupBorderPane();
        setupEscKey();
        primaryStage.setTitle("Klods Hans");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private void setBackGround() {
        background.getGraphicsContext2D().drawImage(bgImage, 0, 0);
    }

    private void setupBorderPane() {
        root.getChildren().add(borderPane);
        VBox vbox = new VBox();
        vbox.setPadding(INSETS);
        vbox.setSpacing(SPACING);
        vbox.setPrefWidth(322);
        Button showCommissions = new MyButton("Varer i Kommission");
        showCommissions.setOnAction(event -> {
            if (!(borderPane.getCenter() instanceof DatabaseView)) {
                borderPane.setCenter(commisions);
                commisions.update();
            }
        });
        Button showOwnedWares = new MyButton("Egne Varer");
        showOwnedWares.setOnAction(event -> {
            if (!(borderPane.getCenter() instanceof DatabaseView)) {
                borderPane.setCenter(ownedWares);
                ownedWares.update();
            }
        });
        Button showSellers = new MyButton("Sælgere");
        showSellers.setOnAction(event -> {
            if (!(borderPane.getCenter() instanceof DatabaseView)) {
                borderPane.setCenter(sellersView);
                sellersView.update();
            }
        });
        Button addSeller = new MyButton("Ny Sælger");
        addSeller.setOnAction(event -> {
            if (!(borderPane.getCenter() instanceof DatabaseView)) {
                borderPane.setCenter(newSellerView);
            }
        });
        Button addWare = new MyButton("Ny Vare");
        addWare.setOnAction(event -> {
            if (!(borderPane.getCenter() instanceof DatabaseView)) {
                borderPane.setCenter(newWareView);
                newWareView.update();
            }
        });
        vbox.getChildren().addAll(new Rectangle(50, 120, Color.TRANSPARENT), showCommissions, showOwnedWares, showSellers, addSeller, addWare);
        borderPane.setLeft(vbox);
    }

    private void setupEscKey() {
        scene.setOnKeyPressed(event -> {
            if (event.getCode().getName().equals("Esc")) {
                Platform.exit();
            }
        });
    }

    @Override
    public void stop() {
        shop.close();
    }

}//End of class
